package testing;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import matching.matchmaker;

public class MatchmakerCreator implements Runnable {

	String configFileS;
	int howManyMatchMakers;
	String httpURL;
	int where;
	boolean debug;
	String supe;
	int portNum;
	int seed;
	

	public MatchmakerCreator(String _configFileS)
	{
		configFileS = _configFileS;
	}

	public void run() {

		Properties configFile = new Properties();
		try {
			configFile.load(new FileInputStream(configFileS));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if(configFile.getProperty("DEBUG").equalsIgnoreCase("true"))
			debug = true;
		else
			debug = false;

		howManyMatchMakers = Integer.parseInt(configFile.getProperty("HOWMANYMATCHMAKERS"));
		httpURL = "http://" + configFile.getProperty("URL") + ":" + configFile.getProperty("HTTPPORT");
		//This is where the matchmaker will start looking in its list starting at zero
		where = Integer.parseInt(configFile.getProperty("WHERE"));


		//getting the multicastPorts
		int multicastPortSM = Integer.parseInt(configFile.getProperty("MULTICASTPORT"));
		int multicastPortG = Integer.parseInt(configFile.getProperty("MulticastPortG"));

		//multicastGroup
		String multicastGroup = configFile.getProperty("MULTICASTGROUP");

		//Where the supervisor is and the portnumber
		portNum = Integer.parseInt(configFile.getProperty("listenPort"));
		supe = configFile.getProperty("supeIP");
		seed = Integer.parseInt(configFile.getProperty("seed"));
		
		for(int i = 0; i < howManyMatchMakers; i++)
		{

			matchmaker maker = new matchmaker(seed, debug, multicastPortG, multicastPortSM, multicastGroup);
			(new Thread(new createMatchmaker(maker))).start();


		}

	}

	public class createMatchmaker implements Runnable
	{
		matchmaker m;
		public createMatchmaker(matchmaker _m)
		{
			m = _m;
		}

		public void run() {
			m.startThread(httpURL, where, supe, portNum);

		}

	}

}
